// src/services/request.js
export const request = {
  // 基础配置
  baseURL: 'http://8080:/SmartA/',
  timeout: 5000,
  
  // 发送请求的核心方法
  send(config) {
    return new Promise((resolve, reject) => {
      // 合并基础配置
      const mergedConfig = {
        url: this.baseURL + config.url,
        method: config.method || 'GET',
        data: config.data || {},
        header: {
          'Content-Type': 'application/json',
          ...config.headers
        },
        timeout: config.timeout || this.timeout,
        success: (res) => {
          // 处理成功响应
          if (res.statusCode >= 200 && res.statusCode < 300) {
            resolve(res.data);
          } else {
            reject(new Error(`请求失败，状态码: ${res.statusCode}`));
          }
        },
        fail: (err) => {
          // 处理请求失败
          reject(err);
        }
      };
      
      // 添加token（如果有）
      // const token = uni.getStorageSync('token');
      // if (token) {
      //   mergedConfig.header['Authorization'] = `Bearer ${token}`;
      // }
      
      // 发送请求
      uni.request(mergedConfig);
    });
  },
  
  // 快捷方法
  get(url, params = {}, config = {}) {
    return this.send({
      ...config,
      url,
      method: 'GET',
      data: params
    });
  },
  
  post(url, data = {}, config = {}) {
    return this.send({
      ...config,
      url,
      method: 'POST',
      data
    });
  },
  
  put(url, data = {}, config = {}) {
    return this.send({
      ...config,
      url,
      method: 'PUT',
      data
    });
  },
  
  delete(url, data = {}, config = {}) {
    return this.send({
      ...config,
      url,
      method: 'DELETE',
      data
    });
  }
};